#ifndef __EMOTIONFILTER_H__
#define __EMOTIONFILTER_H__

#include <map>
#include <vector>

#include <armadillo>

#include "EmCache.h"
#include "GenEEGData.h"
#include "Dsp.h"
/***************************************************
 * mode name:EmotionFilter
 * brief:Emotion_filter, code:UTF-8
 * @author yxt
 * @created 2024/01/16 21:23:57
 * @imfor ~/bigSystem/Emotion/include/EmotionFilter.h
 * @modify:
 *      情绪识别滤波器类，用于对输入的数据进行滤波
 *      依赖库：armadillo、DspFilters
 * 		
***************************************************/
// 滤波器阶数
#define FILTER_ORDER 10
// 陷波器阶数
#define NOTCH_ORDER 4

typedef Dsp::SimpleFilter<Dsp::Butterworth::BandPass<FILTER_ORDER>, 1> PassFilterType;
typedef Dsp::SimpleFilter<Dsp::Butterworth::BandStop<NOTCH_ORDER>, 1> NotchType;

class EmotionFilter
{
    
private:
    // matlab生成的滤波器参数
    std::map<std::string, std::string> EmotionFiltParaMap;

    bool filt_flag;
    int filtType;
    int filtOrder;      // Filter order
    double* filtA = nullptr;
    double* filtB = nullptr;

    // ======================== //
    // elliptic filter
    int _order;             // Filter order
    double _fs;             // Sampling rate (Hz)
    double _lowcut;         // Low cutoff frequency (Hz)
    double _highcut;        // High cutoff frequency (Hz)
    double _ripple_pass;    // Passband ripple (dB)
    double _ripple_stop;    // Stopband attenuation (dB)
    int channalNum;         

private:
    // 缓存
    EmCache m_cache;
    // Dsp::SimpleFilter<Dsp::Elliptic::BandPass<6>, 1> filter;

    // 多通道滤波器，为了兼容多线程，这里一个通道开一个滤波器
    // std::vector<PassFilterType> bandPassFilters = std::vector<PassFilterType>(64);
    // // 陷波器
    // std::vector<NotchType> notch = std::vector<NotchType>(64);

    static std::vector<PassFilterType> bandPassFilters;
    static std::vector<NotchType> notch;

private:
    static void filterChannels(GenEEGData& org_data, GenEEGData& filt_data, int startIndex, int endIndex);
    
public:
    EmotionFilter(std::string filt_fileName);
    EmotionFilter(std::string filt_fileName,bool en_flag, bool refresh_flag);
    EmotionFilter(
                    // int order = 6,
                    double fs = 1000.0,
                    double lowcut = 20,
                    double highcut = 75,
                    int channal_count = 64,
                    double ripple_pass = 1.0,
                    double ripple_stop = 40.0
    );

    ~EmotionFilter();
public:
    void showMap();
    int filt(arma::mat org_data, arma::mat& filt_data);
    // int filt(arma::mat org_data, arma::mat& filt_data, EMF::Filter_TypeDef& filterPara);
    // int filt(arma::mat org_data, arma::mat& filt_data, int empty);
    int filt(GenEEGData& org_data, GenEEGData& filt_data);
    int ellipticFilt(GenEEGData& org_data, GenEEGData& filt_data);
    // 默认使用四线程
    int butterworthFilt(GenEEGData& org_data, GenEEGData& filt_data, uint8_t threadNum = 4);

    bool save(arma::mat &data);
    bool load(arma::mat &data);
};

#endif // !__EMOTIONFILTER_H__
